<?php
namespace PsiFramework\Applications\Bibliotheque\Classes\DAO;

import("Classes.DAO.AbstractDAO");
import("Applications.Bibliotheque.Classes.DAO.ContributionDAO");
import("Applications.Bibliotheque.Classes.DAO.GenreDAO");
import("Applications.Bibliotheque.Classes.DAO.TypeDAO");
import("Applications.Bibliotheque.Classes.Model.Auteur");
import("Applications.Bibliotheque.Classes.Model.Contribution");
import("Applications.Bibliotheque.Classes.Model.Genre");
import("Applications.Bibliotheque.Classes.Model.Type");

use \PsiFramework\Classes\DAO\AbstractDAO;
use \PsiFramework\Applications\Bibliotheque\Classes\DAO\ContributionDAO;
use \PsiFramework\Applications\Bibliotheque\Classes\DAO\GenreDAO;
use \PsiFramework\Applications\Bibliotheque\Classes\DAO\TypeDAO;
use \PsiFramework\Applications\Bibliotheque\Classes\Model\Auteur;
use \PsiFramework\Applications\Bibliotheque\Classes\Model\Contribution;
use \PsiFramework\Applications\Bibliotheque\Classes\Model\Genre;
use \PsiFramework\Applications\Bibliotheque\Classes\Model\Type;

/**
 * Bibliotheque
 * Gestion de ma bibliothèque
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Classe d'accès à une Oeuvre
 *
 * @package DAO
 */
class OeuvreDAO extends AbstractDAO
{

    protected static $_singleton = null;

    /**
     * Retourne toutes les oeuvres
     *
     * @param        $type            Le type de l'oeuvre
     * @param        $orderColumn    La colonne de tri
     * @param        $sens            ascendant ou descendant
     * @return      Oeuvre[]        Toutes le oeuvres
     */
    public function retrieveByType(
        $type=null,
        $orderColumn="oeuvre_titre",
        $sens="ASC"
    )
    {

        //TODO SqlQuery

        $table = null ;
        $column = $orderColumn ;

        $ip = strpos($orderColumn, ".");
        if ($ip != false) {
            $table = substr($orderColumn, 0, $ip);
            $column = substr($orderColumn, $ip+1);
        }

        $whereClausePresent = false ;

        $query = "SELECT oeuvre.* FROM oeuvre" ;
        if ($table != null) {
            if ($table == "auteur") {
                $query .= ", contribution, " . $table ;
                $query .= " WHERE oeuvre.oeuvre_id = contribution.oeuvre_id" ;
                $query .= " AND contribution.auteur_id = auteur.auteur_id" ;

            } else {
                $query .= ", " . $table .
                          " WHERE oeuvre." . $table . "_id = " .
                          $table . "." . $table . "_id" ;
            }
            $whereClausePresent = true ;
        }
        if ($type != null) {
            if (!$whereClausePresent) $query .= " WHERE " ;
            else $query .= " AND " ;

            $query .= "oeuvre.type_id = " . $type->getId();
        }
        $query .= " ORDER BY " ;
        if ($table != null) $query .= $table."." ;
        $query .= $column . " " . $sens ;

        $result = $this->_database->execute($query);
        $oeuvres = array();
        if ($result) {
            while (($obj = $this->_database->getNextResult($result)) != null) {
                $oeuvre = $this->getObjectFromSqlObject($obj);
                $oeuvres[] = $oeuvre;
            }
        }
        return $oeuvres;
    }

    /**
     * Function to be called instead of the constructor.
     *
     * @return OeuvreDAO instance
     */
    public static function getInstance($objectParameters)
    {

        if (self::$_singleton==null) {
            self::$_singleton = new OeuvreDAO($objectParameters);
        }

        return self::$_singleton;
    }
}
